﻿using System.IO;
using Ninject;
using NUnit.Framework;

using System.Net.DnsToolkit.Packet;
using System.Net.DnsToolkit.Serializer;
using System.Net.DnsToolkit.Configuration;
using System.Net.DnsToolkit.Bastard.PacketGenerator;
using System.Net.DnsToolkit.Util;
using System.Net.DnsToolkit.TestUnit.Helper;

namespace System.Net.DnsToolkit.TestUnit.Bastard.PacketGenerator {

    [TestFixture]
    public class SelfRebindingTest {

        [Test]
        public void TestGetMessage() {

            var generator = new SelfRebindingAuthorityAdditional();
            var message = generator.GetMessage();

            Assert.AreEqual(1, message.Questions.Count, "Questions.Count");
            Assert.AreEqual(1, message.Header.QuestionCount, "Header.QuestionCount");

            Assert.AreEqual(0, message.Answers.Count, "Answers.Count");
            Assert.AreEqual(0, message.Header.AnswerCount, "Header.AnswerCount");

            Assert.AreEqual(1, message.Additionals.Count, "Additionals.Count");
            Assert.AreEqual(1, message.Header.ResourceCount, "Header.ResourceCount");

            Assert.AreEqual(1, message.Authorities.Count, "Authorities.Count");
            Assert.AreEqual(1, message.Header.AuthorityCount, "Header.AuthorityCount");

            Assert.AreEqual("darkdns.codeplex.com.", message.Questions[0].QName.Name, "darkdns.codeplex.com.");
            Assert.AreEqual("darkdns.codeplex.com.", message.Additionals[0].Name.Name, "darkdns.codeplex.com.");
            Assert.AreEqual("darkdns.codeplex.com.", message.Authorities[0].Name.Name, "darkdns.codeplex.com.");
        }

        [Test]
        public void TestGetBytes() {


            var generator = new SelfRebindingAuthorityAdditional();
            byte[] bs = generator.GetBytes();

            IMessage original = generator.GetMessage();
            IMessage message = DnsReader.Read(new MemoryStream(bs));


            Assert.AreEqual(1, message.Questions.Count, "Questions.Count");
            Assert.AreEqual(1, message.Header.QuestionCount, "Header.QuestionCount");

            Assert.AreEqual(0, message.Answers.Count, "Answers.Count");
            Assert.AreEqual(0, message.Header.AnswerCount, "Header.AnswerCount");

            Assert.AreEqual(1, message.Additionals.Count, "Additionals.Count");
            Assert.AreEqual(1, message.Header.ResourceCount, "Header.ResourceCount");

            Assert.AreEqual(1, message.Authorities.Count, "Authorities.Count");
            Assert.AreEqual(1, message.Header.AuthorityCount, "Header.AuthorityCount");

            Assert.AreEqual("darkdns.codeplex.com.", message.Questions[0].QName.Name, "darkdns.codeplex.com.");
            Assert.AreEqual("darkdns.codeplex.com.", message.Additionals[0].Name.Name, "darkdns.codeplex.com.");
            Assert.AreEqual("darkdns.codeplex.com.", message.Authorities[0].Name.Name, "darkdns.codeplex.com.");

            MessageHelper.DeepCompare(original, message);
            

        }

    }
}
